package com.andy.beef.login.viewmodel

import com.andy.beef.login.api.loginService
import com.andy.beef.login.bean.TokenBean
import com.andy.beef.login.bean.UserInfoBean
import com.andy.module.constant.AppConstant
import com.andy.module.ext.genLiveData
import com.andy.network.base.BaseViewModel
import com.andy.network.entity.ApiResponse
import com.blankj.utilcode.util.DeviceUtils
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow

/**
 * @creator:LuoFan
 * @date:2022/5/26
 * @description:
 */
class PhoneLoginViewModel : BaseViewModel() {

    private val _tokenState = MutableStateFlow<ApiResponse<TokenBean>>(ApiResponse())

    val tokenState: StateFlow<ApiResponse<TokenBean>> = _tokenState.asStateFlow()

    private val _userInfoState = MutableStateFlow<ApiResponse<UserInfoBean>>(ApiResponse())

    val userInfoState: StateFlow<ApiResponse<UserInfoBean>> = _userInfoState.asStateFlow()

    fun loginByPhone(phone: String, code: String) {
        val params = HashMap<String, Any>()
        params["appId"] = AppConstant.APP_ID
        params["deviceType"] = AppConstant.DEVICE_TYPE
        params["deviceId"] = DeviceUtils.getUniqueDeviceId()
        params["mobile"] = phone
        params["verifyCode"] = code
        makeDialogHttp(_tokenState) {
            loginService.loginByPhone(params)
        }
    }

    fun getUserInfo() {
        val params = HashMap<String, Any>()
        makeDialogHttp(_userInfoState) {
            loginService.getUserInfo(params)
        }
    }
}